package com.cnfangmao.masterdata;

import java.util.ArrayList;
import java.util.List;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.FileOutConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.TemplateConfig;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

public class CodeGenerator {
	/**
	 *	需要重新生成代码时再开启main方法
	 */
//	public static void main(String[] args) {
//		 CodeGenerator codeGen = new CodeGenerator();
//		 codeGen.generateCode();
//	}

	public void generateCode() {
		String packageName = "com.cnfangmao.masterdata";
		// "t_account_role", "t_permission", "t_role", "t_role_permission", "t_account_basic_data_right", "t_basic_data_dependation",
		// "t_basic_data_field_mapping", "t_basic_data_field_metadata", "t_basic_data_metadata",
		// "t_basic_data_metadata_log", "t_basic_data_rule", "t_enum_type", "t_enum_value", "t_field_rule"
		generateByTables(packageName, "t_account_role", "t_permission", "t_role", "t_role_permission");
	}

	private void generateByTables(String packageName, String... tableNames) {

		AutoGenerator autoGen = new AutoGenerator();
		// String dbUrl = "jdbc:mysql://localhost:3306/masterdata?serverTimezone=UTC";
		String dbUrl = "jdbc:mysql://116.62.129.65:3306/masterdata?serverTimezone=UTC";
		DataSourceConfig dataSourceConfig = new DataSourceConfig();
		dataSourceConfig.setDbType(DbType.MYSQL);
		dataSourceConfig.setUrl(dbUrl);
		// dataSourceConfig.setUsername("root");
		// dataSourceConfig.setPassword("abcd1234");
		dataSourceConfig.setUsername("masterdata");
		dataSourceConfig.setPassword("masterdata");
		dataSourceConfig.setDriverName("com.mysql.jdbc.Driver");
		autoGen.setDataSource(dataSourceConfig);

		StrategyConfig strategyConfig = new StrategyConfig();
		strategyConfig.setCapitalMode(true);
		strategyConfig.setEntityLombokModel(false);
		strategyConfig.setEntityTableFieldAnnotationEnable(true);
		strategyConfig.setNaming(NamingStrategy.underline_to_camel);
		strategyConfig.setColumnNaming(NamingStrategy.underline_to_camel);
		strategyConfig.setInclude(tableNames);
		strategyConfig.setTablePrefix(new String[] { "t_" });// 修改替换成你需要的表名，多个表名传数组
		autoGen.setStrategy(strategyConfig);

		GlobalConfig globalConfig = new GlobalConfig();
		String projectPath = System.getProperty("user.dir");
		globalConfig.setActiveRecord(false);
		globalConfig.setAuthor("chenxd");
		globalConfig.setOutputDir(projectPath + "\\src\\main\\java");
		globalConfig.setFileOverride(true);
		globalConfig.setServiceName("%sService");
		globalConfig.setMapperName("%sDao");
		globalConfig.setOpen(false);
		autoGen.setGlobalConfig(globalConfig);

		PackageConfig packageConfig = new PackageConfig();
		packageConfig.setParent(packageName);
		packageConfig.setController("controller");
		packageConfig.setEntity("model");
		packageConfig.setMapper("dao");
		// packageConfig.setXml("dao.mapper");
		autoGen.setPackageInfo(packageConfig);

		InjectionConfig cfg = new InjectionConfig() {
			@Override
			public void initMap() {
			}
		};
		List<FileOutConfig> focList = new ArrayList<FileOutConfig>();
		focList.add(new FileOutConfig("/templates/mapper.xml.vm") {
			@Override
			public String outputFile(TableInfo tableInfo) {
				return projectPath + "\\src\\main\\resources\\mapper\\" + tableInfo.getEntityName() + ".xml";
			}
		});
		cfg.setFileOutConfigList(focList);
		autoGen.setCfg(cfg);
		TemplateConfig tc = new TemplateConfig();
		tc.setXml(null);
		autoGen.setTemplate(tc);

		autoGen.execute();
	}

}
